package com.sasis.dao.ibatis;

import java.util.List;

import org.springframework.orm.ObjectRetrievalFailureException;

import com.sasis.dao.HighRequestDao;
import com.sasis.model.HighRequest;
import com.sasis.util.StringUtil;

public class HighRequestDaoiBatis extends BaseDaoiBATIS implements
		HighRequestDao {

	/**
	 * @see com.sasis.dao.HighRequestDao#getHighRequests(com.sasis.model.HighRequest)
	 */
	public List getHighRequests(final HighRequest highRequest) {
		return getSqlMapClientTemplate().queryForList("getHighRequests", null);
	}

	public List selectHighRequests(final HighRequest highRequest) {
		return getSqlMapClientTemplate().queryForList("selectHighRequests",
				highRequest);
	}

	public List getHighRquestByTaskId(String taskId) {
		return getSqlMapClientTemplate().queryForList("getHighRquestByTaskId",
				taskId);
	}

	/**
	 * @see com.sasis.dao.HighRequestDao#getHighRequest(Integer id)
	 */
	public HighRequest getHighRequest(Integer id) {
		HighRequest highRequest = (HighRequest) getSqlMapClientTemplate()
				.queryForObject("getHighRequest", id);

		if (highRequest == null) {
			throw new ObjectRetrievalFailureException(HighRequest.class, id);
		}

		return highRequest;
	}

	public int saveHighRequestStatus(String requestId) {
		int flag = -1;

		String[] arys = requestId.split(",");
		for (int i = 0; i < arys.length; i++) {
			String reqstId = arys[i];
			HighRequest highRequest = new HighRequest();
			highRequest.setId(!StringUtil.isEmpty(reqstId) ? new Integer(
					reqstId) : new Integer(0));
			int rtnFlag = getSqlMapClientTemplate().update(
					"updateHighRequestStatus", highRequest);
			flag++;
		}

		return flag;

	}

	/**
	 * @see com.sasis.dao.HighRequestDao#saveHighRequest(HighRequest
	 *      highRequest)
	 */
	public void saveHighRequest(final HighRequest highRequest) {
		Integer id = highRequest.getId();
		// check for new record
		if (id == null) {
			id = (Integer) getSqlMapClientTemplate().insert("addHighRequest",
					highRequest);
		} else {
			getSqlMapClientTemplate().update("updateHighRequest", highRequest);
		}
		if (id == null) {
			throw new ObjectRetrievalFailureException(HighRequest.class, id);
		}
	}

	/**
	 * @see com.sasis.dao.HighRequestDao#removeHighRequest(Integer id)
	 */
	public void removeHighRequest(Integer id) {
		getSqlMapClientTemplate().update("deleteHighRequest", id);
	}
}
